package com.aloha.springframework.context.support;

import com.aloha.springframework.beans.BeansException;
import com.aloha.springframework.context.ApplicationContext;
import com.aloha.springframework.beans.factory.config.BeanPostProcessor;
import com.aloha.springframework.context.ApplicationContextAware;

/**
 * @author DaiZhiHeng
 * @description 因为其他的感知器实现类需要获取的内容在BeanFactory那一层已经全部可以获取的到，而applicationContext的对象在那一层是无法获取的到的
 * 所以，可以先创建一个BeanPostProcessor将applicationContext传递进去并注册BeanPostProcessor
 * @date 2023/7/3 9:15
 */
public class ApplicationContextAwareProcessor implements BeanPostProcessor {

    /**
     * 应用上下文对象
     */
    private final ApplicationContext applicationContext;

    public ApplicationContextAwareProcessor(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ApplicationContextAware) {
            ((ApplicationContextAware) bean).setApplicationContext(applicationContext);
        }
        return bean;
    }
}
